﻿using AutoMapper;
using MediatR;
using S.Breeding.Api.Application.Command.OustManageCommand;
using S.Breeding.Domain;
using S.Breeding.Infrastructure;

namespace S.Breeding.Api.Application.CommandHandlers.OustManageCommandHandlers
{
    public class OustManageTrueCommandHandlers : IRequestHandler<OustManageTrueCommand, int>
    {
        private readonly IRepository<OustManage> repository;
        private readonly IMapper mapper;
        public OustManageTrueCommandHandlers(IRepository<OustManage> repository, IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        public async Task<int> Handle(OustManageTrueCommand request, CancellationToken cancellationToken)
        {
            string[] ids = request.Array.Split(',');
            List<OustManage> lists = new List<OustManage>();
            foreach (var item in ids)
            {
                var result = await repository.GetAsync(int.Parse(item));
                if (result.ExamineType == true)
                {
                    return -1;
                }
                result.ExamineType = true;
                result.ReviewBy = "小樱";
                result.ReviewDate = DateTime.Now;
                lists.Add(result);
            }
            foreach (var item in lists)
            {
                await repository.UpdateAsync(item);
            }
            return 1;
        }
    }
}
